//
//  ARTPDFHelper.m
//  ChartsDemo
//
//  Created by 刘权 on 2024/1/13.
//

#import "ARTPDFHelper.h"

@implementation ARTPDFHelper

#pragma mark - 创建PDF储存路径
+ (NSString *)createPDFPathWithName:(NSString *)pdfName {
    NSFileManager * fileManager = [NSFileManager defaultManager];

    NSString * finderPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
                                                                  NSUserDomainMask, YES) lastObject]
                             stringByAppendingPathComponent:@"PDF"];
    
    if (![fileManager fileExistsAtPath:finderPath]){
        [fileManager createDirectoryAtPath:finderPath withIntermediateDirectories:YES
                                attributes:nil
                                     error:NULL];
    }
    return [finderPath stringByAppendingPathComponent:pdfName];
}

#pragma mark - 创建PDF
+ (NSString *)createPDF:(NSArray<UIImage *> *)images {
    NSString * pdfPath = [self createPDFPathWithName:[NSString stringWithFormat:@"睡眠血氧报告_%@.pdf",[[NSDate date] j_stringWithFormat:@"yyyyMMddHHmmss"]]];
 
    // CGRectZero 表示默认尺寸，参数可修改，设置自己需要的尺寸
    UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, NULL);
    
    CGRect  pdfBounds = UIGraphicsGetPDFContextBounds();
    CGFloat pdfWidth  = pdfBounds.size.width;
    CGFloat pdfHeight = pdfBounds.size.height;
    
    [images enumerateObjectsUsingBlock:^(UIImage * _Nonnull image, NSUInteger idx, BOOL * _Nonnull stop) {
        // 绘制PDF
        UIGraphicsBeginPDFPage();
        
        CGFloat imageW = image.size.width;
        CGFloat imageH = image.size.height;
        
        if (imageW <= pdfWidth && imageH <= pdfHeight){
            CGFloat originX = (pdfWidth - imageW) / 2;
            CGFloat originY = (pdfHeight - imageH) / 2;
            [image drawInRect:CGRectMake(originX, originY, imageW, imageH)];
        } else{
            CGFloat width,height;

            if ((imageW / imageH) > (pdfWidth / pdfHeight))
            {
                width  = pdfWidth;
                height = width * imageH / imageW;
            }
            else
            {
                height = pdfHeight;
                width = height * imageW / imageH;
            }
            [image drawInRect:CGRectMake((pdfWidth - width) / 2, (pdfHeight - height) / 2, width, height)];
        }
    }];
    
    UIGraphicsEndPDFContext();
    
    return pdfPath;
}

+ (UIImage *)imageWithTitle:(NSString *)title fontSize:(CGFloat)fontSize image:(UIImage *)image{

    //画布大小

    CGSize size=CGSizeMake(image.size.width,image.size.height);

    //创建一个基于位图的上下文

    UIGraphicsBeginImageContextWithOptions(size,NO,0.0);//opaque:NO  scale:0.0

    

    [image drawAtPoint:CGPointMake(0.0,0.0)];

    

    //文字居中显示在画布上

    NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;

    paragraphStyle.alignment=NSTextAlignmentCenter;//文字居中

    

    //计算文字所占的size,文字居中显示在画布上

    CGSize sizeText=[title boundingRectWithSize:image.size options:NSStringDrawingUsesLineFragmentOrigin

                                     attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSForegroundColorAttributeName:[UIColor grayColor]}context:nil].size;

    CGFloat width = image.size.width;

    CGFloat height = image.size.height;

    

    CGRect rect = CGRectMake((width-sizeText.width)/2, height-sizeText.height, sizeText.width, sizeText.height);//CGRectMake((width-sizeText.width)/2, (height-sizeText.height)/2, sizeText.width, sizeText.height);

    //绘制文字

    [title drawInRect:rect withAttributes:@{ NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSForegroundColorAttributeName:[ UIColor grayColor],NSParagraphStyleAttributeName:paragraphStyle}];

    

    //返回绘制的新图形

    UIImage *newImage= UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;

}


@end
